package 算法回顾;

/**
 * @description: 小白白刷题
 * @author: 小白白
 * @create: 2021-09-08
 **/

public class JZ03数组中重复的数字 {

    public int findRepeatNumber(int[] nums) {

        for (int i = 0; i < nums.length; i++) {
            while (i != nums[i]) {
                int num = nums[i];
                if (nums[num] == num){
                    return num;
                }
                swap(nums,i, num);
            }
        }

        return -1;
    }

    private void swap(int[] arr, int index1 ,int index2) {
        int temp = arr[index1];
        arr[index1] = arr[index2];
        arr[index2] = temp;
    }

    public static void main(String[] args) {
        JZ03数组中重复的数字 n = new JZ03数组中重复的数字();
        int[] arr = {2, 3, 1, 0, 5, 1};
        int result = n.findRepeatNumber(arr);
        System.out.println(result);
    }

}
